<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <new-page-filter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.list"
      :filter-btn="filterInfo.btn"
    >
    </new-page-filter>
    <dialog-table
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      :listen-height="false"
      :tab-index="tabIndex"
      :query="filterInfo.query"
      :field-list="
        changeContract ? tableInfo.changeFieldList : tableInfo.fieldList
      "
      :selectable="selectable"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="!changeContract && checkBox"
      :checkedList="checkBox ? checkedList : []"
      :pager="changeContract ? false : true"
      @handleEvent="handleEvent"
    />
  </page-dialog>
</template>

<script setup name="selectContractNoCustomize">
const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: true,
  },
  checkBox: {
    type: Boolean,
    default: false,
  },
  selectable: {
    type: Function,
  },
  tabIndex: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  checkedList: {
    type: Array,
    default: function () {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  data: {
    type: Array,
    default: function () {
      return []
    },
  },
  changeContract: {
    type: Boolean,
    default: false,
  },
})

// 过滤相关配置
const filterInfo = ref({
  query: proxy.$fn.deepClone(props.query),
  copyQuery: {},
  list: [
    {
      componentType: 'input',
      label: '设备编码',
      value: 'tsCodeLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '设备名称',
      value: 'tsNameLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '设备型号',
      value: 'tsStandardLike',
      event: search,
    },
  ],
  btn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  copyData: [],
  selections: [],
  fieldList: [
    { label: '设备编码', value: 'tsCode' },
    { label: '设备名称', value: 'tsName' },
    { label: '设备型号', value: 'tsStandard' },
    { label: '厂家', value: 'tsFactory' },
  ],
  changeFieldList: [
    { label: '设备编码', value: 'tsCode' },
    { label: '设备名称', value: 'tsName' },
    { label: '设备型号', value: 'tsStandard' },
    { label: '厂家', value: 'tsFactory' },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: '选择设备',
  btLoading: false,
  width: '800px',
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')

const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})

onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    getList()
  })
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
})

// 获取列表
function getList() {
  tableInfo.value.api = props.api
  tableInfo.value.refresh = Math.random()
}
function clearSearch() {
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  search()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function save() {
  handleClose()
  emits('callback', selectRow.value)
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !(
          selectRow.value &&
          (selectRow.value.length || selectRow.value.id)
        )
      }
      break
    case 'tableCheck':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !(
          selectRow.value &&
          (selectRow.value.length || selectRow.value.id)
        )
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
}
</script>

<style scoped></style>
